package cn.com.gridinfo.sooc.core.login.dao;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.JsonUtil;
import com.jeremy.arad.utils.Log;
import com.jeremy.arad.utils.StringUtils;
import com.loopj.android.http.RequestParams;

import java.io.IOException;
import java.util.List;

import cn.com.gridinfo.sooc.base.http.SoocIDao;
import cn.com.gridinfo.sooc.base.http.SoocNetResult;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.common.PublicConst;
import cn.com.gridinfo.sooc.common.UrlConst;
import cn.com.gridinfo.sooc.core.login.bean.NodeServerInfo;
import cn.com.gridinfo.sooc.core.login.bean.UserInfo;
import cn.com.gridinfo.sooc.core.login.common.CodeConst;
import cn.com.gridinfo.sooc.utils.HttpUrl;
import cn.com.gridinfo.sooc.utils.MD5;

/**
 * 登陆 dao
 * Created by jieli on 15/8/24.
 */
public class LoginDao extends SoocIDao {
    public LoginDao(SoocNetResult activity){
        super(activity);
    }

    public String status;//状态 0 成功 1 服务器错误  2 逻辑错误
    public String statusCode;//提示信息

    private int serverCount;//服务器节点数量
    private List<NodeServerInfo> nodeServerList; //节点服务器列表
    private UserInfo userInfo;//登录返回用户信息

    @Override
    public void onRequestSuccess(JsonNode result, int requestCode) throws IOException {

        if (result != null) {
            Log.d("result:["+result+"]");
            //登录获取节点服务器是没有如下两个字段的
            if(requestCode != CodeConst.LOGIN_NODE_SERVER){
                status = result.get("status").asText();
                statusCode = result.get("statusCode").asText();
                if (!status.equals("0")) {
                   Log.d("服务器返回错误信息: "+status+":"+statusCode);
                    return;
                }
            }
        } else {
            return;
        }


        switch (requestCode) {
            case CodeConst.LOGIN_NODE_SERVER: //登录获取节点服务器
                serverCount = result.get("count").asInt();
                nodeServerList =  JsonUtil.node2pojoList(result.get("lists"), NodeServerInfo.class);
              break;
            case CodeConst.LOGIN_VALIDATE_LOGIN://输入账号密码登录成功
                userInfo = JsonUtil.node2pojo(result.get("data"),UserInfo.class);
                //存储 tocken 数据
                Arad.preferences.putString(LocalMark.OUATH_TOKEN.getName(),result.get("oauth_token").asText());
                Arad.preferences.putString(LocalMark.OAUTH_TOKEN_SECRET.getName(),result.get("oauth_token_secret").asText());
                Arad.preferences.flush();

                break;
            case CodeConst.LOGIN_IS_PERMISION: //获取用户权限
                userInfo = JsonUtil.node2pojo(result.get("data"),UserInfo.class);
                break;
        }

    }


    /**
     * 根据账号获取节点服务器列表
     * @param account
     */
    public void getNodeServer(String account){

        String url = PublicConst.CLOUD_URL+"index.php?m=api&c=Index&a=getSnsList";
        RequestParams param = new RequestParams();
        param.add("account", account);
        postRequest(url,param,CodeConst.LOGIN_NODE_SERVER,PublicConst.NOT_NEED_TOKEN);
    }


    /**
     * 用户登录
     * @param account
     * @param pwd
     */
    public void loginUser(String account, String pwd){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.Oauth.toString(),"authorize",false);
        RequestParams param = new RequestParams();
        param.add("email",account);//账号
        param.add("pwd", MD5.encode(StringUtils.changeSpecialChar(pwd)));//密码 md5加密后
        param.add("soocdevice",Arad.app.deviceInfo.getDeviceMode());
        param.add("soocversion",Arad.app.deviceInfo.getOsSystemVer());
        param.add("soocimei",Arad.app.deviceInfo.getDeviceImsi());

        postRequest(url,param,CodeConst.LOGIN_VALIDATE_LOGIN,PublicConst.NOT_NEED_TOKEN);

    }

    /**
     * 获取用户权限  是否游客身份 积分墙是否显示 广告是否显示 1 是 0 否
     */
    public void getPermision(String channelNo){
        String url = PublicConst.CLOUD_URL+ "/index.php/api/Index/show_wp";
        RequestParams param = new RequestParams();
        param.add("code",channelNo);//渠道号
        param.add("snsid",Arad.preferences.getString(LocalMark.SERVER_ID.getName()));//服务器ID

        postRequest(url,param,CodeConst.LOGIN_IS_PERMISION,PublicConst.NOT_NEED_TOKEN);

    }

    /**
     * 极光推送设备绑定
     */
    public void bindJpushAlias(){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(),"bindDevice",true);
        Log.d(url);
        RequestParams param = new RequestParams();
        param.add("uid",Arad.preferences.getString(LocalMark.USER_UID.getName()));//用户 uid
        param.add("snsid",Arad.preferences.getString(LocalMark.SERVER_ID.getName()));//服务器ID
        param.add("type","android");//移动端设备类型
        String deviceInfo = Arad.app.deviceInfo.getDeviceID()+" | "+ Arad.app.deviceInfo.getDeviceImsi()+" | "+ Arad.app.deviceInfo.getDeviceMode()+" | "+android.os.Build.BRAND;
        param.add("device",deviceInfo);//手机信息
        postRequest(url,param,CodeConst.LOGIN_JPUSH_BIND,PublicConst.NOT_NEED_TOKEN);

    }

    /**
     * 极光推送设备解绑
     */
    public void  unBindJpushAlias(){
        String url = HttpUrl.getHttpUrl(UrlConst.Module.User.toString(),"unbindDevice",true);
        RequestParams param = new RequestParams();
        param.add("uid",Arad.preferences.getString(LocalMark.USER_UID.getName()));//用户 uid
        postRequest(url,param,CodeConst.LOGIN_JPUSH_UNBIND,PublicConst.NOT_NEED_TOKEN);

    }




    public int getServerCount() {
        return serverCount;
    }

    public List<NodeServerInfo> getNodeServerList() {
        return nodeServerList;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

}
